﻿using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using ALinq.BBS.DataAccess;
using ALinq.BBS.Models;
using ALinq.BBS.App_GlobalResources;

namespace ALinq.BBS.Controllers
{
    public partial class HomeController : Controller
    {
        public ActionResult Authenticate()
        {
            if (!base.User.Identity.IsAuthenticated)
            {
                return Redirect(this.CreateLoginPageUrl(Request.RawUrl));
            }
            return null;
        }

        public ActionResult Index()
        {
            var boards = new Boards();
            return View(boards);
        }

        public ActionResult PostList(int boardID, int forumID, int? pageIndex)
        {
            var boards = new Boards();
            var board = boards.Single(o => o.ID == boardID);
            var forum = board.Forums.Single(o => o.ID == forumID);
            var postList = forum.Posts.GetList(pageIndex.GetValueOrDefault());

            dynamic obj = new ExpandoObject();
            obj.Board = board;
            obj.Forum = forum;
            obj.PostList = postList;

            return View(obj);
        }

        public ActionResult Post(int boardID, int forumID, int postListIndex, int postID, int? pageIndex)
        {
            var boards = new Boards();
            var board = boards.Single(o => o.ID == boardID);
            var forum = board.Forums.Single(o => o.ID == forumID);
            var post = forum.Posts.FindItems(o => o.ID == postID).SingleOrDefault();
            var replyList = post.Replies.GetList(pageIndex.GetValueOrDefault());

            dynamic obj = new ExpandoObject();

            ViewBag.Board = board;
            ViewBag.Forum = forum;
            ViewBag.Post = post;
            ViewBag.PostListIndex = postListIndex;
            //obj.ReplyList = replyList;

            return View(replyList);
        }




        public ActionResult Message(MessageCode code)
        {
            var message = ClientMessage.CodeToString(code);

            dynamic obj = new ExpandoObject();
            obj.Message = message;
            obj.Success = false;

            if (code >= MessageCode.Success)
            {
                obj.Success = true;
            }

            return View(obj);
        }

        public ActionResult Login()
        {
            return View();
        }



        public ActionResult DeletePost()
        {
            return View();
        }


    }



}
